package com.hugoo.extra.xlsx;

import com.hugoo.core.util.TL;
import java.io.IOException;
import java.io.Writer;

/**
 *
 * @author Lovelock.Chan
 */
class Ssw {

    private final Writer _out;
    private int _rownum;
    private StringBuilder sb;

    public Ssw(Writer out) {
        this._out = out;
    }

    public void beginSheet() throws IOException {
        this._out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");

        this._out.write("<sheetData>\n");
    }

    public void endSheet() throws IOException {
        this._out.write("</sheetData>");
        this._out.write("</worksheet>");
        this._out.flush();
        this._out.close();
    }

    /**
     * 本处设计为 起始值为 1
     *
     * @param rownum
     * @throws IOException
     */
    public void insertRow(int rownum)
            throws IOException {
        this._out.write("<row r=\"" + rownum + "\">\n");
        this._rownum = rownum;
    }

    public void endRow()
            throws IOException {
        this._out.write("</row>\n");
    }

    /**
     * 列设置 起始值为 0
     *
     * @param columnIndex
     * @param value
     * @param styleIndex
     * @throws IOException
     */
    public void createCell(int columnIndex, String value, int styleIndex) throws IOException {
        String ref = cntd(this._rownum, columnIndex);//.formatAsString();
        this._out.write("<c r=\"" + ref + "\" t=\"inlineStr\"");
        if (styleIndex != -1) {
            this._out.write(" s=\"" + styleIndex + "\"");
        }
        this._out.write(">");
        this._out.write("<is><t>" + value + "</t></is>");
        this._out.write("</c>");
    }

    public void createCell(int columnIndex, String value) throws IOException {
        createCell(columnIndex, value, -1);
    }

    public static String cntd(int row, int col) {
        int excelColNum = col + 1;

        String colRef = "";
        int colRemain = excelColNum;

        while (colRemain > 0) {
            int thisPart = colRemain % 26;
            if (thisPart == 0) {
                thisPart = 26;
            }
            colRemain = (colRemain - thisPart) / 26;

            char colChar = (char) (thisPart + 64);
            colRef = colChar + colRef;
        }
        return colRef + row;
    }

    public void createCell(int columnIndex, double value, int styleIndex) throws IOException {
        String ref = cntd(this._rownum, columnIndex);
        this._out.write("<c r=\"" + ref + "\" t=\"n\"");
        if (styleIndex != -1) {
            this._out.write(" s=\"" + styleIndex + "\"");
        }
        this._out.write(">");
        this._out.write("<v>" + value + "</v>");
        this._out.write("</c>");
    }

    public void createCell(int columnIndex, double value) throws IOException {
        createCell(columnIndex, value, -1);
    }
}

class Ssw2 {

    private final Writer _out;
    private int _rownum;
    private StringBuilder sb;

    public Ssw2(Writer out) {
        this._out = out;
    }

    public void beginSheet() throws IOException {
        this._out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><sheetData>\n");
    }

    public void endSheet() throws IOException {
        this._out.write("</sheetData></worksheet>");
        this._out.flush();
        this._out.close();
    }

    /**
     * 本处设计为 起始值为 1
     *
     * @param rownum
     * @throws IOException
     */
    public void insertRow(int rownum)
            throws IOException {
        sb = new StringBuilder();
        sb.append("<row r=\"").append(rownum).append("\">\n");
        this._rownum = rownum;
    }

    public void endRow()
            throws IOException {
        sb.append("</row>\n");
        this._out.write(sb.toString());
    }

    /**
     * 列设置 起始值为 0
     *
     * @param columnIndex
     * @param value
     * @param styleIndex
     * @throws IOException
     */
    public void createCell(int columnIndex, String value, int styleIndex) throws IOException {
        String ref = cntd(this._rownum, columnIndex);//.formatAsString();
        sb.append("<c r=\"").append(ref).append("\" t=\"inlineStr\"");
        if (styleIndex != -1) {
            sb.append(" s=\"").append(styleIndex).append("\"");
        }
        sb.append(">").append("<is><t>").append(TL.escape(value)).append("</t></is>").append("</c>");
    }

    public void createCell(int columnIndex, String value) throws IOException {
        createCell(columnIndex, value, -1);
    }

    public static String cntd(int row, int col) {
        int excelColNum = col + 1;

        String colRef = "";
        int colRemain = excelColNum;

        while (colRemain > 0) {
            int thisPart = colRemain % 26;
            if (thisPart == 0) {
                thisPart = 26;
            }
            colRemain = (colRemain - thisPart) / 26;

            char colChar = (char) (thisPart + 64);
            colRef = colChar + colRef;
        }
        return colRef + row;
    }

    public void createCell(int columnIndex, double value, int styleIndex) throws IOException {
        String ref = cntd(this._rownum, columnIndex);
        sb.append("<c r=\"").append(ref).append("\" t=\"n\"");
        if (styleIndex != -1) {
            sb.append(" s=\"").append(styleIndex).append("\"");
        }
        sb.append(">").append("<v>").append(value).append("</v>").append("</c>");
    }

    public void createCell(int columnIndex, double value) throws IOException {
        createCell(columnIndex, value, -1);
    }
}
